package com.perceptivesoftware.licensing.client.views.mostrecent;

import java.util.ArrayList;
import java.util.List;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.perceptivesoftware.licensing.client.LicensingAsyncCallback;
import com.perceptivesoftware.licensing.client.LicensingRPCFactory;
import com.perceptivesoftware.licensing.client.dto.VideoDTO;
import com.perceptivesoftware.licensing.client.dto.VideoModel;

public class RecentVideosPanel extends ContentPanel
{
	private Grid<VideoModel> grid;
	private ListStore<VideoModel> store;

	public RecentVideosPanel()
	{
		buildUI();
	}

	public void loadData()
	{
		LicensingRPCFactory.getTwistageService(true).getAllVideos(new LicensingAsyncCallback<List<VideoDTO>>() {

			@Override
			public void onSuccess(final List<VideoDTO> result)
			{
				loadRecentVideos(result);
			}
		});
	}

	public void buildUI()
	{
		//		setHeaderVisible(false);
		setHeading("Most Recent Videos");
		setLayout(new FitLayout());
		buildGrid();
		add(grid);
	}

	private void buildGrid()
	{
		final List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		final ColumnConfig nameColumn = new ColumnConfig();
		nameColumn.setId("title");
		nameColumn.setHeader("Title");
		nameColumn.setWidth(100);
		configs.add(nameColumn);
		
		final ColumnConfig thumbColumn = new ColumnConfig();
		thumbColumn.setId("thumb");
		thumbColumn.setHeader("");
		thumbColumn.setWidth(200);
		configs.add(thumbColumn);

		final ColumnModel cm = new ColumnModel(configs);

		store = new ListStore<VideoModel>();

		grid = new Grid<VideoModel>(store, cm);
		grid.setAutoExpandColumn("title");
		grid.setHeight(400);
		grid.setBorders(true);
		grid.setStripeRows(true);

		grid.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener<VideoModel>() {
			@Override
			public void selectionChanged(final SelectionChangedEvent<VideoModel> se)
			{
				itemSelected(se.getSelectedItem());
			}

		});
		grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
	}

	/**
	 * Called by the selection listener to determine the selected item in the tree node.
	 * 
	 * @param item
	 */
	public void itemSelected(final VideoModel item)
	{
		if (item != null)
		{

		}
	}

	/**
	 * Called by the presenter class to load the header list retrieved from the server.
	 * 
	 * @param result
	 */
	public void loadRecentVideos(final List<VideoDTO> result)
	{
		store.removeAll();
		for (final VideoDTO vid : result)
		{
			store.add(new VideoModel(vid));
		}
		store.sort("name", SortDir.ASC);
		store.commitChanges();
	}
}